package com.example.newapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.newapplication.SealFragment.BackFragment;
import com.example.newapplication.SealFragment.BrowFragment;
import com.example.newapplication.SealFragment.BuyFragment;
import com.example.newapplication.SealFragment.FixFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class Message extends AppCompatActivity {

    private TextView title,num;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments;
    private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        init();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

//        TabLayout.Tab tab = tabLayout.newTab().setText("借出").setCustomView(R.layout.red_point_layout);
//        tabLayout.addTab(tab);

        TabLayout.Tab tab1 = tabLayout.newTab();
        tab1.setCustomView(R.layout.red_point_layout);
        title = tab1.getCustomView().findViewById(R.id.textView106);
        num = tab1.getCustomView().findViewById(R.id.iv_tab_red);
        title.setText("借出");
        num.setText("3");
        tabLayout.addTab(tab1);

        TabLayout.Tab tab2 = tabLayout.newTab();
        tab2.setCustomView(R.layout.red_point_layout);
        title = tab2.getCustomView().findViewById(R.id.textView106);
        num = tab2.getCustomView().findViewById(R.id.iv_tab_red);
        title.setText("归还");
        num.setText("2");
        tabLayout.addTab(tab2);

        TabLayout.Tab tab3 = tabLayout.newTab();
        tab3.setCustomView(R.layout.red_point_layout);
        title = tab3.getCustomView().findViewById(R.id.textView106);
        num = tab3.getCustomView().findViewById(R.id.iv_tab_red);
        title.setText("报修");
        num.setText("2");
        tabLayout.addTab(tab3);

        TabLayout.Tab tab4 = tabLayout.newTab();
        tab4.setCustomView(R.layout.red_point_layout);
        title = tab4.getCustomView().findViewById(R.id.textView106);
        num = tab4.getCustomView().findViewById(R.id.iv_tab_red);
        title.setText("订购");
        num.setText("5");
        tabLayout.addTab(tab4);

        fragments = new ArrayList<>();
        fragments.add(new BrowFragment());
        fragments.add(new BackFragment());
        fragments.add(new FixFragment());
        fragments.add(new BuyFragment());

        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
                // tabLayout.setBackgroundColor(getResources().getColor(R.color.white));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // tabLayout.setBackgroundColor(getResources().getColor(R.color.white));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // tabLayout.setBackgroundColor(getResources().getColor(R.color.white));
            }
        });
    }
    private void init() {
        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
        back = findViewById(R.id.back);
    }
    private class FragmentAdapter extends FragmentPagerAdapter {

        List<Fragment> fragments2;
        public FragmentAdapter(@NonNull FragmentManager fm, int behavior, ArrayList<Fragment> fragments) {
            super(fm, behavior);
            this.fragments2 = fragments;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments2.get(position);
        }

        @Override
        public int getCount() {
            return fragments2.size();
        }
    }
}